// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationData: {},
    pageHeight:null,
    comment:"",
    showWritenCard:false,
    showWritenBox:false,
    selectedPost:null,
    width:"100rpx",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let source = options.source
    let app = getApp()
    if(source == "search"){
      let selectedSearchPost = app.globalData.selectedSearchPost
      wx.request({
        url: app.globalData.url + '/community/informpost',
        data:{
          openId: selectedSearchPost.item.author,
          postId: selectedSearchPost.item.id,
        },
        
        success: result => {
          let post = result.data.data
          if(post.imgPath != ""){
            post.imgPath = post.imgPath.split(",")
            for(let j = 0; j < post.imgPath.length; ++j){
              post.imgPath[j] = app.globalData.url + "/" + post.imgPath[j]
            }
          }else{
            post.imgPath = []
          }
          app.globalData.selectedPost = post
          this.setData({selectedPost:post})
          this.increaseReadNum()
        }
      })

    }else{
      let selectedPost = app.globalData.selectedPost
      this.setData({selectedPost:selectedPost})
      this.increaseReadNum()
    }
    
  },

  increaseReadNum (){
    
    let app = getApp()
    wx.request({
      url: app.globalData.url + '/community/read',
      data:{
        "postId" : this.data.selectedPost.id,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: result => {
      }
    })
    
  },

  onReady: function () {
    this.animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease',
    })
    let height = wx.getSystemInfoSync().windowHeight
    let width = wx.getSystemInfoSync().windowWidth
    let imgWidth = parseInt((width - 50) / 3)
    this.setData({
      pageHeight:height + "px",
      width:imgWidth + "px"
    })

  },

  onShow: function () {

  },
  previewImgs:function(e){
    
    let current = e.currentTarget.dataset.index
    wx.previewImage({
      current: this.data.selectedPost.imgPath[current],
      urls: this.data.selectedPost.imgPath
    })
  },
  submitComment:function(e){
    let commentText = e.detail.value.textarea
    let app = getApp()
    if(commentText != "")
    {
      wx.request({
        url: app.globalData.url + '/community/comment',
        data:{
          openId: app.globalData.userInfo.openid,
          commentText:commentText,
          postId:this.data.selectedPost.id
        },
        success: result => {
          if(result.data.status == 200){
            let commentObj = {
              "userAvatarUrl": app.globalData.userInfo.avatarUrl,
              "userName": app.globalData.userInfo.nickName,
              "likeNum": 0,
              "commentText": commentText,
            }
            let selectedPost = this.data.selectedPost
            selectedPost.commentsList.push(commentObj)
            selectedPost.commentsNum = selectedPost.commentsNum + 1
            this.setData({
              selectedPost:selectedPost,
            })
            this.refreshComments(this.data.selectedPost.id)
          }
        }
      })
    }
    this.setData({showWritenCard:false})
    this.setData({showWritenBox:false})
  },
  refreshComments:function(postId){
    let app = getApp()
    wx.request({
      url: app.globalData.url + '/community/getcomment',
      data:{
        postId:postId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: result => {
        let commentsList = result.data.data
        let selectedPost = this.data.selectedPost
        selectedPost.commentsList = commentsList
        this.setData({
          selectedPost:selectedPost
        })

      }
    })
  },
  translate: function () {
    let app = getApp()
    if(app.globalData.newUser){
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        duration: 1000
      })  
    }else{
      let height = wx.getSystemInfoSync().windowHeight;
      this.setData({showWritenCard:true,showWritenBox:true})
      this.animation.translateY(-height*0.8).step()
      this.setData({animation: this.animation.export()})
    }
    
  },
  hide: function () {
    let height = wx.getSystemInfoSync().windowHeight;
    this.setData({showWritenCard:false})
    this.animation.translateY(height*0.8).step()
    this.setData({animation: this.animation.export()})
    setTimeout(() => {
      this.setData({showWritenBox:false})
    }, 400)
  },
  thumbsUp:function(e){
    
    let app = getApp()
    if(app.globalData.newUser){
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        duration: 1000
      })  
    }else{
      let commentIndex = e.currentTarget.dataset.commentIndex
      let commentId = e.currentTarget.dataset.commentId
      if(this.data.selectedPost.commentsList[commentIndex].liked == 0){
        wx.request({
          url: app.globalData.url + '/community/like',
          data:{
            commentId:commentId,
            likeNum:this.data.selectedPost.commentsList[commentIndex].likeNum+1
          },
          success: result => {
            if(result.data.status == 200){
              let selectedPost = this.data.selectedPost
              selectedPost.commentsList[commentIndex].likeNum ++
              selectedPost.commentsList[commentIndex].liked = 1
              this.setData({
                selectedPost:selectedPost
              })
            }
          }
        })
      }
    }
  },
  
  onHide: function () {

  },
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})